﻿using Caliburn.Micro;

namespace SS.WPF.Framework.Localization
{
    using System;
    using System.ComponentModel;
    using System.Windows;

    /// <summary>
    /// The class that is used to receive events from LanguageChangedEventManager through WeakEvent pattern
    /// </summary>
    public class TranslationData : IWeakEventListener, INotifyPropertyChanged
    {
        private string _key;

        public string Key
        {
            get { return this._key; }
            set { this._key = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TranslationData"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        public TranslationData(string key)
        {
            _key = key;
            this.ListeningChangedLanguageEvent();
        }

        ///
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// is reclaimed by garbage collection.
        ///
        ~TranslationData()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Code to dispose the managed resources of the class
                LanguageChangedEventManager.RemoveListener(IoC.Get<TranslationManager>(), this);
            }
            // Code to dispose the un-managed resources of the class
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


        public object Value
        {
            get { return this._key == null ? null : this.GetTranslatedData(_key); }
        }

        #region IWeakEventListener Members

        public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
        {
            var result = false;
            if (managerType == typeof (LanguageChangedEventManager))
            {
                OnLanguageChanged(sender, e);
                result = true;
            }
            return result;
        }

        private void OnLanguageChanged(object sender, EventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Value"));
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Help Methods

        /// <summary>
        /// Register listening weak event for manager
        /// </summary>
        private void ListeningChangedLanguageEvent()
        {
            LanguageChangedEventManager.AddListener(IoC.Get<TranslationManager>(), this);
        }

        /// <summary>
        /// Using TranslationManage as provider to get translated data
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private object GetTranslatedData(string key)
        {
            return IoC.Get<TranslationManager>().Translate(key);
        }

        #endregion
    }
}
